import 'dart:collection';
import 'package:atcflutter/base/network/base_entry.dart';
import 'package:zcore/http/http_util.dart';
import 'package:dio/dio.dart';
import 'package:zcore/utils/security_util.dart';

class ATCHttpUtil {
  static final String APP_KEY = "com.che168.www";

  static final String KEY_APP_ID = "_appid";
  static final String VALUE_APP_ID = "atc.android";

  static final String KEY_CHANNEL_ID = "channelid";
  static final String VALUE_CHANNEL_ID = "csy";

  static final String KEY_APP_VERSION = "appversion";
  static final String KEY_UDID = "udid";
  static final String KEY_SIGN = "_sign";

  static final String KEY_USERKEY = "userkey";

  /// 设置本机代理 127.0.0.1:8888
  static void setProxy(String proxy) {
    HttpUtil.default_proxy = proxy;
  }

  /// 设置超时时间（s）
  static void setConnectTimeOut(int second) {
    HttpUtil.getDio().options.connectTimeout = second * 1000;
  }

  /// get请求
  static Future<dynamic> get<T>(String url, Map<String, dynamic> params) {
    return _handleResultCode<T>(HttpUtil.get(url, _handleCommonParams(params)));
  }

  /// post请求
  static Future<dynamic> post<T>(String url, Map<String, dynamic> params) {
    return _handleResultCode<T>(HttpUtil.post(url, _handleCommonParams(params)));
  }

  /// 请求结果预处理
  static Future<dynamic> _handleResultCode<T>(Future<dynamic> future) {
    return future.then((result) {
      final returnCode = result["returncode"];
      if(returnCode == 0){
        //成功
        return Future.value(result);
      }else {
        //失败
        return Future.error(result);
      }
    }).catchError((error){
      BaseEntry entry = BaseEntry();
      entry.returncode = -1;//非正常返回错误码
      switch(error.type){
        case DioErrorType.CONNECT_TIMEOUT:
        case DioErrorType.SEND_TIMEOUT:
        case DioErrorType.RECEIVE_TIMEOUT:
          //统一为连接超时
          entry.message = "网络连接超时，请检查网络后重试！";
          break;
        case DioErrorType.RESPONSE:
          entry.message = "服务器出问题了！";
          break;
        case DioErrorType.CANCEL:
          entry.message = "请求被取消！";
          break;
        case DioErrorType.DEFAULT:
          entry.message = "其它未知错误！";
          break;
      }
      return Future.error(entry);
    });
  }

  /// 公共参数处理
  static Map<String, dynamic> _handleCommonParams(Map<String, dynamic> params) {
    Map<String, dynamic> result = SplayTreeMap();
    //1.首先添加公共参数
    result[KEY_APP_ID] = VALUE_APP_ID;
    result[KEY_CHANNEL_ID] = VALUE_CHANNEL_ID;
    result[KEY_APP_VERSION] = "1.9.88"; //待获取APP版本
    result[KEY_UDID] = "asdfsfdsafdasdfsafdaf"; //待获取UDID
//    result[KEY_USERKEY] = "s";//待判断用户userkey
    //2.添加自定义参数
    result.addAll(params);
    //3.参数加密
    result[KEY_SIGN] = _toSign(result, APP_KEY);
    return result;
  }

  /// 参数加密处理
  static String _toSign(Map<String, dynamic> paramMap, String appKey) {
    String result = "";
    if (paramMap != null) {
      result += appKey;
      paramMap.forEach((key, value) {
        result += key + value.toString();
      });
      result += appKey;
    }
    return SecurityUtil.generateMd5(result).toUpperCase();
  }
}
